package pikasial.graphique;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;

import pikasial.images.PikImage;

public class ProprietesImageDialog extends JDialog {

	private static final long serialVersionUID = 4737890830245341873L;
	private PikImage image;
	private JTabbedPane tabPane;
	private JScrollPane propPane;
	private JScrollPane exifPane;
	private JButton ok;
	private Font bold,plain;
	private java.text.SimpleDateFormat dateFormat;

	public ProprietesImageDialog(PikImage pik) {
		super(pikasial.Pikasial.getFrame(),true);
		image = pik;
		setTitle("Propriétés de l'image "+image.getName());
		JPanel pane = new JPanel(new BorderLayout());
		dateFormat = new java.text.SimpleDateFormat("d/M/y");
		setContentPane(pane);
		initComponents();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setPreferredSize(new java.awt.Dimension(800,600));
		pack();
		setVisible(true);
	}

	private void initComponents() {
		JPanel boutonPanel = new JPanel();
		ok = new JButton("Ok",new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.OK_ICON_PATH)));
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				dispose();
			}
		});
		boutonPanel.add(ok);

		bold = new Font(null,Font.BOLD,12);
		plain = new Font(null,Font.PLAIN,12);
		propPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		exifPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		tabPane = new JTabbedPane(JTabbedPane.TOP);

		/* **** Propriétés image **** */
		JPanel prop = new JPanel(new BorderLayout());
		JPanel head = new JPanel();
		JPanel desc = new JPanel();
		head.setLayout(new GridBagLayout());
		desc.setLayout(new GridBagLayout());

		JLabel thumb = new JLabel();
		JLabel title = new JLabel();
		try {
			thumb.setIcon(image.getVignetteIcon());
		} catch (java.io.IOException ioe) {
			thumb.setIcon(null);
			thumb.setText("Image indisponible");
		}
		thumb.setBackground(Color.WHITE);
		thumb.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.WHITE, Color.BLACK));
		thumb.setVerticalAlignment(JLabel.CENTER);
		thumb.setHorizontalAlignment(JLabel.CENTER);
		title.setText(image.getName());
		title.setVerticalAlignment(JLabel.CENTER);
		title.setHorizontalAlignment(JLabel.CENTER);
		title.setFont(bold.deriveFont((float) 26.0));

		GridBagConstraints gc1 = new GridBagConstraints();
		//gc1.fill = GridBagConstraints.HORIZONTAL;
		gc1.gridx = 0;
		gc1.gridy = 0;
		gc1.weightx = 0.20;
		gc1.insets = new Insets(10,10,40,10);
		GridBagConstraints gc2 = new GridBagConstraints();
		//gc2.fill = GridBagConstraints.HORIZONTAL;
		gc2.gridx = 1;
		gc2.gridy = 0;
		gc2.insets = new Insets(10,10,40,10);
		gc2.ipadx = 10;
		gc2.fill = GridBagConstraints.HORIZONTAL;
		gc2.weightx = 0.8;
		JPanel panlThumb = new JPanel();
		panlThumb.add(thumb);
		head.add(panlThumb,gc1);
		head.add(title,gc2);

		JLabel label, value;
		gc1 = new GridBagConstraints();
		gc2 = new GridBagConstraints();
		gc1.gridx = 0;
		gc1.gridy = 0;
		gc1.weightx = 0.35;
		gc1.fill = GridBagConstraints.HORIZONTAL;
		gc1.insets = new Insets(10,20,10,20);
		gc2.gridx = 1;
		gc2.gridy = 0;
		gc2.fill = GridBagConstraints.HORIZONTAL;
		gc2.insets = new Insets(10,20,10,20);

		label = new JLabel("Chemin d'accès :");
		label.setFont(bold);
		value = new JLabel(image.getPath());
		value.setFont(plain);
		desc.add(label,gc1);
		desc.add(value,gc2);
		label = new JLabel("Taille :");
		label.setFont(bold);
		long taille = image.getJavaFile().length();
		String lab;
		if (taille > 1048576) {
			lab = ""+taille+" octets ("+((double) taille)/1048576+" Mo)";
		} else
			lab = ""+taille+" octets ("+taille/1024+" Ko)";

		value = new JLabel(lab);
		value.setFont(plain);
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		desc.add(label,gc1);
		desc.add(value,gc2);

		label = new JLabel("Date de prise :");
		label.setFont(bold);
		String s = null;
		try {
			s = dateFormat.format(image.getDatePrise());
		} catch (Exception e) {}
		value = new JLabel((s==null)?"Inconnue":s);
		value.setFont(plain);
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		desc.add(label,gc1);
		desc.add(value,gc2);

		label = new JLabel("Date de modification :");
		label.setFont(bold);
		value = new JLabel(dateFormat.format(image.getDateModification()));
		value.setFont(plain);
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		desc.add(label,gc1);
		desc.add(value,gc2);

		label = new JLabel("Longueur :");
		label.setFont(bold);
		int width = 0;
		try {
			width = (int) image.getDimensions().getWidth();
		} catch (java.io.IOException e) {
			width = 0;
		}
		value = new JLabel((width==0)?"Inconnue":(""+width+" pixels"));
		value.setFont(plain);
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		desc.add(label,gc1);
		desc.add(value,gc2);

		label = new JLabel("Largeur :");
		label.setFont(bold);
		int height = 0;
		try {
			height = (int) image.getDimensions().getHeight();
		} catch (java.io.IOException e) {
			height = 0;
		}
		value = new JLabel((height==0)?"Inconnue":(""+height+" pixels"));
		value.setFont(plain);
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		desc.add(label,gc1);
		desc.add(value,gc2);

		String comment = image.getCommentaire();
		if (comment != null) {
			label = new JLabel("Commentaire :");
			label.setFont(bold);
			JTextArea text = new JTextArea();
			text.setLineWrap(true);
			text.setWrapStyleWord(true);
			text.setEditable(false);
			text.setText(comment);
			text.setBackground(desc.getBackground());
			gc1 = (GridBagConstraints) gc1.clone();
			gc1.gridy++;
			gc2 = (GridBagConstraints) gc2.clone();
			gc2.gridy++;
			desc.add(label,gc1);
			desc.add(text,gc2);
		}

		Collection<pikasial.images.tags.Tag> c = image.getTags().getAllTags();
		if (c.size() != 0) {
			ArrayList<pikasial.images.tags.TagPerso> tagsPerso = new ArrayList<pikasial.images.tags.TagPerso>();
			for (pikasial.images.tags.Tag t : c) {
				if (t instanceof pikasial.images.tags.TagLieu) {
					label = new JLabel("Lieu :");
					label.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PLACE_TAG_24_ICON_PATH)));
				} else if (t instanceof pikasial.images.tags.TagPersonne) {
					label = new JLabel("Personne(s) :");
					label.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PEOPLE_TAG_24_ICON_PATH)));
				} else if (t instanceof pikasial.images.tags.TagEvenement) {
					label = new JLabel("Evenement :");
					label.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.EVENT_TAG_24_ICON_PATH)));
				} else if (t instanceof pikasial.images.tags.TagPerso)
					tagsPerso.add((pikasial.images.tags.TagPerso) t);
				if (! (t instanceof pikasial.images.tags.TagPerso)) {
					label.setFont(bold);
					value = new JLabel(t.getDescription());
					value.setFont(plain);
					gc1 = (GridBagConstraints) gc1.clone();
					gc1.gridy++;
					gc2 = (GridBagConstraints) gc2.clone();
					gc2.gridy++;
					desc.add(label,gc1);
					desc.add(value,gc2);
				}
			}
			if (tagsPerso.size() != 0) {
				label = new JLabel("Autres Tags :");
				label.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PERSO_TAG_24_ICON_PATH)));
				label.setFont(bold);
				StringBuffer stb = new StringBuffer("");
				stb.append(tagsPerso.get(0).getDescription());
				for (int i=1; i < tagsPerso.size(); i++) {
					stb.append("\n"+tagsPerso.get(i));
				}
				value = new JLabel(stb.toString());
				value.setFont(plain);
				gc1 = (GridBagConstraints) gc1.clone();
				gc1.gridy++;
				gc2 = (GridBagConstraints) gc2.clone();
				gc2.gridy++;
				desc.add(label,gc1);
				desc.add(value,gc2);
			}
		}

		label = new JLabel("");
		value = new JLabel("");
		gc1 = (GridBagConstraints) gc1.clone();
		gc1.gridy++;
		gc2 = (GridBagConstraints) gc2.clone();
		gc2.gridy++;
		gc1.insets = new Insets(10,20,30,20);
		gc2.insets = new Insets(10,20,30,20);
		desc.add(label,gc1);
		desc.add(value,gc2);

		prop.add(head,BorderLayout.NORTH);
		prop.add(desc,BorderLayout.CENTER);
		propPane.setViewportView(prop);

		/*  **** Données EXIF **** */
		JPanel exif = new JPanel();
		exif.setLayout(new GridBagLayout());
		boolean noExif = false;

		try {
			Set<String> keys = image.getExifData().getTagNameSet();
			gc1 = new GridBagConstraints();
			gc2 = new GridBagConstraints();
			gc1.gridx = 0;
			gc1.gridy = -1;
			gc1.weightx = 0.35;
			gc1.fill = GridBagConstraints.HORIZONTAL;
			gc1.insets = new Insets(10,20,10,20);
			gc2.gridx = 1;
			gc2.gridy = -1;
			gc2.fill = GridBagConstraints.HORIZONTAL;
			gc2.insets = new Insets(10,20,10,20);
			for (String str : keys) {
				label = new JLabel(str);
				label.setFont(bold);
				value = new JLabel(image.getExifData().getDescription(str));
				value.setFont(plain);
				gc1 = (GridBagConstraints) gc1.clone();
				gc1.gridy++;
				gc2 = (GridBagConstraints) gc2.clone();
				gc2.gridy++;
				exif.add(label,gc1);
				exif.add(value,gc2);
			}
			exifPane.setViewportView(exif);
		} catch (Exception e) {
			noExif = true;
		}

		tabPane.add(propPane,"Propriétés de l'image");
		tabPane.add(exifPane,"Données EXIF");
		if (noExif)
			tabPane.setEnabledAt(1, false);
		getContentPane().add(tabPane,BorderLayout.CENTER);
		getContentPane().add(boutonPanel,BorderLayout.SOUTH);

	}

	public int getSelectedTab() {
		return tabPane.getSelectedIndex();
	}

	public void setSelectedTab(int index) {
		tabPane.setSelectedIndex(index);
	}
}
